#include "resource.h"
#include "util.h"
#include <cstdarg>

namespace NS_TIEC {
  HashMap<const char*, const char*> Resources::string_map_;

  const char* Resources::getChars(const char* key) {
    if (string_map_.empty()) {
      initDefaultStrings();
    }
    auto it = string_map_.find(key);
    if (it == string_map_.end()) {
      return {};
    }
    return it->second;
  }

  const char* Resources::getFormatChars(const char* key, ...) {
    if (string_map_.empty()) {
      initDefaultStrings();
    }
    auto it = string_map_.find(key);
    if (it == string_map_.end()) {
      return {};
    }
    va_list args;
    va_start(args, key);
    const char* res = formatChars(it->second, args);
    va_end(args);
    return res;
  }

  String Resources::getString(const char* key) {
    return getChars(key);
  }

  String Resources::getFormatString(const char* key, ...) {
    va_list args;
    va_start(args, key);
    const char* res = getFormatChars(key, args);
    va_end(args);
    return res;
  }

  void Resources::putString(const char* name, const char* value) {
    if (string_map_.empty()) {
      initDefaultStrings();
    }
    string_map_[name] = value;
  }

  void Resources::initDefaultStrings() {
    putString("diagnostic.warning", "警告：%s");
    putString("diagnostic.error", "错误：%s");
    putString("diagnostic.info.full", "%s:信息(%uld,%uld): %s");
    putString("diagnostic.warning.full", "%s:警告(%uld,%uld): %s");
    putString("diagnostic.error.full", "%s:错误(%uld,%uld): %s");
    putString("parse.expected", "此处需要 %s");
    putString("parse.unexpected", "此处不应该出现 %s");
    putString("parse.package.define.duplicate", "一个文件内只能定义一个包名，请删除多余的包名定义");
  }
}